package com.origon.managedbeans;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.origon.entities.Address;
import com.origon.entities.Country;
import com.origon.entities.Language;
import com.origon.entities.User;
import com.origon360.Services.RegistrationService;
import com.origon360.Services.ShopService;
import com.origon360.utilities.NavigationHelper;

@ManagedBean(name="registrationBean")
@ViewScoped
public class RegistrationBean {
	
	@EJB 
	private RegistrationService registrationService;
	
	@EJB
	private ShopService shopService;
	
	Logger log = Logger.getLogger(RegistrationBean.class);
	
	private static Log logg = LogFactory.getLog(RegistrationBean.class);
	
	private User user ;
	
	private List<Language> languages;
	
	private List<Country> countryList;
	
	private Language lang;
	
	private boolean sameAddress;
	
	public RegistrationBean() {
		init();
	}

	public void init() {
		user = new User();
		user.setBillingAddress(new Address());
		user.setShippingAddress(new Address());
		sameAddress = true;
	}
	
	public Language getLang() {
		return lang;
	}

	public void setLang(Language lang) {
		this.lang = lang;
	}

	public boolean isSameAddress() {
		return sameAddress;
	}

	public void setSameAddress(boolean sameAddress) {
		this.sameAddress = sameAddress;
	}

	private List<User> usersList = new ArrayList<User>();	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	} 
	

	public List<Language> getLanguages() {
		if(languages == null)
		{
			languages = registrationService.fetchLaguages();
		}
		
		return languages;
	}

	public void setLanguages(List<Language> languages) {
		this.languages = languages;
	}

	public List<Country> getCountryList() {
		if(countryList == null)
		{
			countryList = shopService.fetchCountries();
		}
		return countryList;
	}

	public void setCountryList(List<Country> countryList) {
		this.countryList = countryList;
	}

	public List<User> getUsersList() {
			 initUsers(); 
		return usersList;
	}

	public void setUsersList(List<User> usersList) {
		this.usersList = usersList;
	}

	public String navigateToUsers(){
		return NavigationHelper.USERS_URI;
	}
	
	private void initUsers(){
		usersList = registrationService.fetchUsers();
	}
	
	
	
	public void addMessage(String msg){
		FacesMessage facesMessage = new FacesMessage(msg);
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
	}
	
	public void register(ActionEvent actionEvent){
		try {
			
			if(sameAddress)
			{
				user.setShippingAddress(user.getBillingAddress());
			}
			
			
			Timestamp date = new Timestamp(System.currentTimeMillis());
			
			user.setRegistrationDate(date);
			log.info("Registering user "+user.getUsername()+" to system");
			
			logg.info("Registering user "+user.getUsername()+" to system (from common Log)");
			
			String message = "Registration was successful";
			addMessage(message);
			
		    registrationService.createUser(user);
			
		} catch (Exception e) {
			String message = "Registration Failed";
			addMessage(message);
		}
		
		
		
	}
	
	
}
